package net.dreamlu.qa.validator;

import net.dreamlu.qa.model.User;
import net.dreamlu.util.MD5Util;

import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;

public class LoginValidator extends Validator {
	
	protected void validate(Controller controller) {
		setShortCircuit(true);
		validateEmail("user.email", "errorMail", "1");
		validateString("user.password", 6, 20, "errorPwd", "1");
		User user = User.dao.findFirst("select  1 from user_info where email = ? and password = ?", controller.getPara("user.email"), MD5Util.getMD5Str(controller.getPara("user.password")));
		if(user == null){
			addError("errorPwd", "1");
		}		
	}
	
	protected void handleError(Controller controller) {
		controller.keepModel(User.class);
		controller.render("/account/login.jsp");
	}
}